﻿using Microsoft.AspNetCore.Mvc;
using PMS.Core.Configuration.CommonEnumConfigs;
using PMS.Core.Utils;
using PMS.Services.EntityServices.NaviMenus;
using PMS.Services.Utils.Security;
using PMS.Web.Factories.NaviMenus;
using PMS.Web.Framework.Controllers;
using PMS.Web.Models.NaviMenus;
using System;

namespace PMS.Web.Controllers.WebApi.AdminArea.NaviMenus
{
    public class NaviMenuWebApi : BaseApiController
    {
        #region Fields
        private readonly INaviMenuService _naviMenuService;
        private readonly IPermissionService _permissionService;
        private readonly INaviMenuModelFactory _naviMenuModelFactory;
        #endregion

        #region Ctor
        public NaviMenuWebApi(INaviMenuService naviMenuService,
            IPermissionService permissionService,
            INaviMenuModelFactory naviMenuModelFactory)
        {
            _naviMenuService = naviMenuService;
            _permissionService = permissionService;
            _naviMenuModelFactory = naviMenuModelFactory;
        }
        #endregion

        #region Methods
        [HttpPost(WebApiRouteConfig.LoadPagedNaviMenu)]
        public IActionResult LoadPagedNaviMenu([FromBody] NaviMenuSearchModel searchModel)
        {
            try
            {
                var pagedMenus = _naviMenuModelFactory.PrepareNaviMenuEditListModel(searchModel);
                return SuccessJson("", pagedMenus);
            }
            catch (PMSException pex)
            {
                return pex.LogLevel switch
                {
                    SystemLogLevel.Warning => WarnJson(pex.Message),
                    _ => ErrorJson(pex.Message)
                };
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }
        #endregion
    }
}
